<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{User as Users, Angts, Quotes, Order};
use think\Facade\Config as Configs;
use think\Db;
use app\facade\Redis;
use think\facade\Debug;


class Quote extends Command
{
    private $url = 'https://api-aws.huobi.pro/';

    protected function configure()
    {
        $this->setName('Quote')->setDescription('所有币种行情跑数据');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        $cmd = "ps aux | grep '/www/server/php/72/bin/php /www/wwwroot/JTE/think Jtequotes'|grep -v 'grep'| awk '{print $2}'";
        exec($cmd, $ps, $ret);
        if (count($ps) > 1) {
            die("batch of  is running");
        }
        ini_set('max_execution_time', 0);
        $list = (new Quotes())->where('ishow', 1)->select();
        $data = [];
        foreach ($list as $lists) {
            $coin = $lists['coin'];
            $data[$lists['id']] = [
                'id' => $lists['id'],
                'coins' => strtoupper($coin),
                'mart' => strtoupper($lists['mart']),
                'ishot' => $lists['ishot'],
                'high' => 0,
                'low' => 0,
                'vol' => 0, //成交量
                'close' => 0, //最新价
                'open' => 0, //开盘价
                'quto' => 0, //开盘价
            ];
            if ($lists['type'] == 2) {
                //通过订单记录获取24小时行情
                $time = date('Y-m-d');
                $coinorder = (new Order())
                    ->order('clostime desc')
                    ->field('transnum,transprice')
                    ->where('clostime', '>=', $time)
                    ->where(['coin' => $lists['coin'], 'mart' => $lists['mart'], 'status' => 2])
                    ->select();
                $ordercount = count($coinorder);
                if (!empty($coinorder) && $ordercount > 0) {
                    $data[$lists['id']]['open'] =  $coinorder[($ordercount - 1)]['transprice'];
                    $data[$lists['id']]['close'] =  $coinorder[0]['transprice'];
                    if ($data[$lists['id']]['open'] > 0) {
                        $data[$lists['id']]['quto'] = floor(($data[$lists['id']]['close'] - $data[$lists['id']]['open']) * 1000000 / $data[$lists['id']]['open']) / 10000; //开盘价
                    }
                    $volnum = 0;
                    $maxprice = 0;
                    $minprice = 0;
                    for ($i = 0; $i < $ordercount; $i++) {
                        $volnum += $coinorder[$i]['transnum'];
                        if ($coinorder[$i]['transprice'] > $maxprice) {
                            $maxprice = $coinorder[$i]['transprice'];
                        }
                        if ($coinorder[$i]['transprice'] < $minprice || $minprice == 0) {
                            $minprice = $coinorder[$i]['transprice'];
                        }
                    }
                    $data[$lists['id']]['high'] =  $maxprice;
                    $data[$lists['id']]['low'] =  $minprice;
                    $data[$lists['id']]['vol'] =  $volnum;
                }
            } else {
                $url = $this->url . "market/history/kline?symbol=" . strtolower($lists['coin'] . $lists['mart']) . '&period=1day&size=1';
                $result = api_curl($url);
                $client = json_decode($result, true);
                if (!empty($client['status']) && $client['status'] == 'ok') {
                    $data[$lists['id']] = [
                        'id' => $lists['id'],
                        'coins' => strtoupper($coin),
                        'mart' => strtoupper($lists['mart']),
                        'ishot' => $lists['ishot'],
                        'high' => $client['data'][0]['high'],
                        'low' => $client['data'][0]['low'],
                        'vol' => $client['data'][0]['vol'], //成交量
                        'close' => $client['data'][0]['close'], //最新价
                        'open' => $client['data'][0]['open'], //开盘价
                        'quto' => floor(($client['data'][0]['close'] - $client['data'][0]['open']) * 1000000 / $client['data'][0]['open']) / 10000, //开盘价
                    ];
                }
            }
        }
        $data = array_merge($data);
        $last_names = array_column($data, 'quto');
        array_multisort($last_names, SORT_DESC, $data);
        Redis::setex('quotekline', 86400, serialize($data));
    }
}
